package com.dingding.service.user.core.car;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.daox.IBaseDaoX;
import com.dingding.common.core.base.daox.ISQLBaseDao;
import com.dingding.common.core.beans.carinfo.CarBrandInfo;
import com.dingding.common.core.beans.carinfo.CarModelInfo;
import com.dingding.common.core.exception.bussiness.BizExpEnum;
import com.dingding.common.core.exception.bussiness.BussinessException;
import com.dingding.common.core.util.car.FetchCarInfoApi;
import com.dingding.facade.user.dto.car.CarModelDTO;
import com.dingding.facade.user.dto.car.ResGetCarBrands;
import com.dingding.facade.user.dto.car.ResGetCarModels;

/**
 * @Description:汽车品牌模型服务类
 * @author fengshuonan
 * @date 2016年10月28日 下午4:02:44
 * @version 叮叮搭车
 * @since 2.4
 */
@Component("carBrandModelDao")
public class CarBrandModelDao {

	@Resource(name = "baseDaoX")
	private IBaseDaoX baseDaoX;
	
	@Resource(name = "SQLBaseDao")
	private ISQLBaseDao SQLBaseDao;
	
	/**
	 * @Description:把一个车辆品牌信息入库
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public void brandToDb(CarBrandInfo brandInfo){
		
		//查询数据库有没有这个车辆的品牌
		CarBrandInfo info = baseDaoX.getById(brandInfo.getId(), CarBrandInfo.class);
		if(info == null){
			this.baseDaoX.save(brandInfo);
		}
	}
	
	/**
	 * @Description:把某个品牌的车辆型号信息入库
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public void modelToDb(Integer brandId,CarModelInfo carModelInfo){
		
		//查询数据库有没有这个车辆的型号
		CarModelInfo info = baseDaoX.getById(carModelInfo.getId(), CarModelInfo.class);
		if(info == null){
			carModelInfo.setBrandId(brandId);
			this.baseDaoX.save(carModelInfo);
		}
	}
	
	/**
	 * @Description:所有的车辆品牌和车辆型号信息入库
	 * @author fengshuonan
	 * @since 2.4
	 */
	public void fetchToDb(){
		FetchCarInfoApi fetchCarInfoApi = new FetchCarInfoApi();
		
		//查询所有的车辆品牌
		List<CarBrandInfo> brands = fetchCarInfoApi.fetchAllBrands();
		
		//查询对应品牌的所有车辆
		for(CarBrandInfo brandInfo : brands){
			
			//把该品牌入库
			brandToDb(brandInfo);
			
			//获取该品牌系列下的所有车型
			List<CarModelInfo> fetchAllModels = fetchCarInfoApi.fetchAllModels(brandInfo.getId());
			
			for(CarModelInfo carModelInfo : fetchAllModels){
				
				//把该车型入库
				modelToDb(brandInfo.getId(),carModelInfo);
			}
		}
	}
	
	/**
	 * @Description:增量获取车辆的品牌
	 * @param currentCusCarBrandNum 客户端的
	 * @author fengshuonan
	 * @return ResGetCarBrands
	 * @since 2.4
	 */
	@Transactional(readOnly = true)
	public ResGetCarBrands getCarBrands(Integer currentCusCarBrandNum){
		
		ResGetCarBrands response = new ResGetCarBrands();
		ArrayList<CarBrandInfo> newBrands = new ArrayList<CarBrandInfo>();
		
		//获取当前所有的车辆的品牌的数量
		List<CarBrandInfo> queryCriteriaList = this.SQLBaseDao.listSqlToCustomBean(
				CarBrandInfo.class, "select * from carbrandinfo c order by c.id ASC");
		
		if(queryCriteriaList == null){
			throw new BussinessException(BizExpEnum.GET_BRANDS_ERROR);
		}
		
		int allBrandsNum = queryCriteriaList.size();
		response.setCarBrandNum(allBrandsNum);
		
		//如果客户端的数量小于当前服务器的车辆品牌数量
		if(currentCusCarBrandNum < allBrandsNum){
			for(int i=allBrandsNum-1;i>=currentCusCarBrandNum;i--){
				newBrands.add(queryCriteriaList.get(i));
			}
		}
		
		response.setCarBrandList(newBrands);
		
		return response;
	}
	
	/**
	 * @Description:获取车辆的所有型号
	 * @param carBrandId 车辆品牌id
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Transactional(readOnly = true)
	public ResGetCarModels getCarModels(Integer carBrandId){
		
		ResGetCarModels response = new ResGetCarModels();
		List<CarModelDTO> carModelList = null;
		
		//获取车辆品牌id所对应的所有的车辆型号集合
		carModelList = this.SQLBaseDao.listSqlToCustomBean(
				CarModelDTO.class, "select c.id,c.name from carmodelinfo c where c.brandId = ?",carBrandId);
		
		if(carModelList == null || carModelList.size() == 0){
			throw new BussinessException(BizExpEnum.GET_BRANDS_ERROR);
		}
		response.setCarModelList(carModelList);
		
		return response;
	}
}
